package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    //处理查询所有
    //@RequiresPermissions("employee:list")
    //@RequiredPermission(name = "员工列表", expression = "employee:list")
    @RequestMapping("/list")
    public String list (Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);

        return "/employee/list";//找视图
    }

    //新增或编辑页面
    //@RequiredPermission(name = "员工编辑", expression = "employee:input")
    @RequestMapping("/input")
    public String input (Model model, Long id) {
        model.addAttribute("departments", departmentService.listAll());

        model.addAttribute("roles", roleService.listAll());

        if (id != null) {
            model.addAttribute("employee", employeeService.selectById(id));
        }
        return "/employee/input";//找视图

    }

    //删除操作
    //@RequiredPermission(name = "员工删除", expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete (Long id) {
        if (id != null) {
            employeeService.deleteById(id);
        }
        return "redirect:/employee/list.do";//让浏览器重新发一次请求
    }

    //新增/修改
    //@RequiredPermission(name = "员工保存或更新", expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate (Employee employee, Long[] ids) {

        if (employee.getId() == null) {
            employeeService.save(employee, ids);
        } else {
            employeeService.updateById(employee, ids);
        }
        return new JsonResult();

    }

    //批量删除
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete (Long[] ids) {
        employeeService.batchDelete(ids);
        return new JsonResult();

    }

    //验证用户名
    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName (String name, Long id) {
        HashMap<Object, Object> map = new HashMap<>();

            Employee employee = employeeService.selectByName(name);
            Employee selectById = employeeService.selectById(id);
            if (id != null) {
                if (selectById.getName().equals(name)) {
                    map.put("valid", true);
                    return map;
                } else {
                    map.put("valid", employee == null);
                    return map;
                }
        } else {
            map.put("valid", employee == null);
            return map;
        }
    }


    //@RequiredPermission(name = "员工删除", expression = "employee:delete")


    @RequestMapping("/status")
    public String status (Long id) {
        if (id != null) {
            //根据传过来的id值获取当前需要操作的员工对象
            Employee employee = employeeService.selectById(id);
            employeeService.updateStatus(id, employee.isStatus());
        }
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/exportXls")
    public void exportXls (HttpServletResponse response) throws IOException {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");

        Workbook workbook = employeeService.exportXls();
        workbook.write(response.getOutputStream());
    }


    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls (MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }


}
